package com.jky.trial.service.impl;

import com.jky.trial.common.Constants;
import com.jky.trial.common.DateUtil;
import com.jky.trial.common.ListResult;
import com.jky.trial.common.Pagination;
import com.jky.trial.constant.DataStatus;
import com.jky.trial.entity.Record;
import com.jky.trial.entity.RecordExample;
import com.jky.trial.entity.Task;
import com.jky.trial.mapper.ProjectMapper;
import com.jky.trial.mapper.RecordMapper;
import com.jky.trial.mapper.TaskMapper;
import com.jky.trial.service.RecordService;
import com.jky.trial.vo.RecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class RecordServiceImpl implements RecordService {
    private final RecordMapper recordMapper;
    private final TaskMapper taskMapper;
    private final ProjectMapper projectMapper;


    @Autowired
    RecordServiceImpl(
            RecordMapper recordMapper,
            ProjectMapper projectMapper,
            TaskMapper taskMapper
    ) {

        this.projectMapper = projectMapper;
        this.taskMapper = taskMapper;
        this.recordMapper = recordMapper;
    }

    @Override
    public String holeStartEnd(int taskId) {
        RecordVo taskVo = new RecordVo();
        taskVo.setSize(Integer.MAX_VALUE);
        List<RecordVo> list1 = list(taskVo);
        list1 = list1.stream().sorted((u1, u2) -> u1.getCreateAt().compareTo(u2.getCreateAt())).collect(Collectors.toList());

        return
                DateUtil.dateToStr(list1.get(0).getCreateAt(), "yyyy/MM/dd") + "," +
                        DateUtil.dateToStr(list1.get(list1.size() - 1).getCreateAt(), "yyyy/MM/dd")
                ;
    }

    @Override
    public int create(Record record) {
        Task task = taskMapper.selectByPrimaryKey(record.getTargetId());
        if (task != null) {
            Record record1 = new Record();
            record1.setTargetId(record.getTargetId());
            if (record.getType().equals(DataStatus.LOG_SUB_TYPE_RECORD_DONGTAN.getVal()) ||
                    record.getType().equals(DataStatus.LOG_SUB_TYPE_RECORD_DONGTAN.getVal()) ||
                    record.getType().equals(DataStatus.LOG_SUB_TYPE_RECORD_QUTU.getVal())) {
                record1.setType(record.getType());
            }
            int num = (int) this.count(record1);
            record.setNo(
                    task.getNo() +
                            //Constants.generateLimitNumberStr(task.getNo(), 4) +
                            "-" +
                            (num + 1)
                    //                 Constants.generateLimitNumberStr(num + 1, 2)
            );

            record.setCreateAt(new Date());
            record.setProjectId(task.getProjectId());
        }
        return recordMapper.insertSelective(record);
    }

    @Transactional
    @Override
    public int createHuiCi(RecordVo record) {
        Task task = taskMapper.selectByPrimaryKey(record.getTargetId());
        if (task != null) { //
            Record record1 = new Record();
            record1.setTargetId(record.getTargetId());
            int num = (int) this.count(record1);
            record.setNo(
                    Constants.generateLimitNumberStr(task.getNo(), 4) +
                            "-" +
                            Constants.generateLimitNumberStr(num + 1, 2));
            record.setCreateAt(new Date());
            record.setProjectId(task.getProjectId());
        }
        record.setCreateAt(new Date());
        recordMapper.insertSelective(record); //回次
        if (record.getBiaoguan() != null && record.getBiaoguan().size() != 0) {
            record.getBiaoguan().forEach(item -> {
                this.setNo(item, task, record.getId(), DataStatus.LOG_SUB_TYPE_RECORD_BIAOGUAN.getVal());

                recordMapper.insertSelective(item);
            });
        }
        if (record.getQutu() != null && record.getQutu().size() != 0) {
            record.getQutu().forEach(item -> {
                this.setNo(item, task, record.getId(), DataStatus.LOG_SUB_TYPE_RECORD_QUTU.getVal());

                recordMapper.insertSelective(item);
            });
        }
        if (record.getDongtan() != null && record.getDongtan().size() != 0) {
            record.getDongtan().forEach(item -> {
                this.setNo(item, task, record.getId(), DataStatus.LOG_SUB_TYPE_RECORD_DONGTAN.getVal());

                recordMapper.insertSelective(item);
            });
        }
        return 0;
    }

    @Transactional
    public int updateHuiCi(RecordVo record) {
        record.setCreateAt(new Date());
        recordMapper.updateByPrimaryKeySelective(record); //回次
        Task task = taskMapper.selectByPrimaryKey(record.getId());
        if (record.getBiaoguan() != null && record.getBiaoguan().size() != 0) {
            record.getBiaoguan().forEach(item -> {
                if (item.getId() == null) {
                    this.setNo(item, task, record.getId(), DataStatus.LOG_SUB_TYPE_RECORD_BIAOGUAN.getVal());
                    recordMapper.insertSelective(item);
                } else {
                    recordMapper.updateByPrimaryKeySelective(item);

                }
            });
        }
        if (record.getQutu() != null && record.getQutu().size() != 0) {
            record.getQutu().forEach(item -> {
                if (item.getId() == null) {
                    this.setNo(item, task, record.getId(), DataStatus.LOG_SUB_TYPE_RECORD_QUTU.getVal());

                    recordMapper.insertSelective(item);
                } else {
                    recordMapper.updateByPrimaryKeySelective(item);
                }
            });
        }
        if (record.getDongtan() != null && record.getDongtan().size() != 0) {
            record.getDongtan().forEach(item -> {
                if (item.getId() == null) {
                    this.setNo(item, task, record.getId(), DataStatus.LOG_SUB_TYPE_RECORD_DONGTAN.getVal());
                    recordMapper.insertSelective(item);
                } else {
                    recordMapper.updateByPrimaryKeySelective(item);
                }
            });
        }
        return 0;
    }

    public void setNo(Record item, Task task, Integer huiciId, Integer type) {
        item.setTargetId(task.getId());
        item.setProjectId(task.getProjectId());
        item.setHcId(huiciId);
        Record record1 = new Record();
        record1.setTargetId(task.getId());
        record1.setType(type);
        int num = (int) this.count(record1);
        item.setNo(
                Constants.generateLimitNumberStr(task.getNo(), 4) +
                        "-" +
                        Constants.generateLimitNumberStr(num + 1, 2));
        item.setCreateAt(new Date());
    }

    @Override
    public int update(Record record) {
        RecordExample recordExample = new RecordExample();
        RecordExample.Criteria criteria = recordExample.createCriteria();
        criteria.andIdEqualTo(record.getId());//查询条件
        return recordMapper.updateByExampleSelective(record, recordExample);
    }

    @Override
    public int del(int id) {
        return recordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Record quTuDetail(int id) {
        RecordVo record = recordMapper.selectByPrimaryKey(id);              //
        if (record.getProjectId() != null) {
            record.setProjectName(projectMapper.selectByPrimaryKey(record.getProjectId()).getName());
        }
        if (record.getTargetId() != null) {
            record.setTask(taskMapper.selectByPrimaryKey(record.getTargetId()));
        }
        return record;
    }

    @Override
    public Record detail(int id) {
        RecordVo record = recordMapper.selectByPrimaryKey(id);              //
        if (record.getType().equals(DataStatus.LOG_SUB_TYPE_RECORD_HUICI.getVal())) {
            //标贯
            Record recordVo = new Record();
            recordVo.setSize(Integer.MAX_VALUE);
            recordVo.setType(DataStatus.LOG_SUB_TYPE_RECORD_BIAOGUAN.getVal());
            recordVo.setHcId(record.getId());
            recordVo.setTargetId(record.getTargetId());
            recordVo.setProjectId(record.getProjectId());
            record.setBiaoguan(recordMapper.selectBy(recordVo));
            recordVo.setType(DataStatus.LOG_SUB_TYPE_RECORD_DONGTAN.getVal());
            record.setDongtan(recordMapper.selectBy(recordVo));
            recordVo.setType(DataStatus.LOG_SUB_TYPE_RECORD_QUTU.getVal());
            record.setQutu(recordMapper.selectBy(recordVo));
        }

        Task task = taskMapper.selectByPrimaryKey(record.getTargetId());    //
        record.setTask(task);                                               //
        return record;
    }


    @Override
    public List<RecordVo> list(RecordVo record) {
        return recordMapper.pageList(record);
    }

    @Override
    public List<RecordVo> listWithTask(RecordVo record) {
        return recordMapper.pageListWithTask(record);
    }

    @Override
    public ListResult page(RecordVo record) {
        List<RecordVo> list = list(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public ListResult projectPage(RecordVo record) {
        List<RecordVo> list = list(record);
        ListResult res = new ListResult(true, list);
        res.setPage(new Pagination(
                record.getPage(),
                (int) count(record),
                record.getSize()
        ));
        return res;
    }

    @Override
    public long count(Record record) {
        RecordExample recordExample = new RecordExample();
        RecordExample.Criteria criteria = recordExample.createCriteria();
        criteria.andDeletedEqualTo(false);//
        if (record.getTargetId() != null) {
            criteria.andTargetIdEqualTo(record.getTargetId());
        }
        if (record.getProjectId() != null) {
            criteria.andProjectIdEqualTo(record.getProjectId());
        }
        if (record.getType() != null) {
            criteria.andTypeEqualTo(record.getType());
        }
        return recordMapper.countByExample(recordExample);
    }

    @Override
    public Float selectMaxDeep(Record record) {
        return recordMapper.selectMaxDeep(record);
    }
}
